package Clothing.serviceImpl.customer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import Clothing.bean.request.customer.AddUserRequest;
import Clothing.bean.request.customer.EditUserRequest;
import Clothing.bean.request.customer.GetUserListRequest;
import Clothing.bean.respose.customer.CustomerResponse;
import Clothing.bean.respose.customer.CustomerResponse.UserInfo;
import Clothing.dao.UserDao;
import Clothing.entity.UserEntity;
import Clothing.service.customer.CustomerService;

@Service("customerService")
public class CustomerSerivceImpl implements CustomerService {

    @Autowired
    private UserDao userDao;
    
    public int getUserList(GetUserListRequest request, CustomerResponse response) {
        Map<String, Object> inputMap = new HashMap<String, Object>();
        inputMap.put("userName", request.getUserName());
        if(StringUtils.isNotBlank(request.getSex())){
            inputMap.put("sex", Integer.parseInt(request.getSex()));
        }
        List<UserEntity> userEntities = userDao.getUserList(inputMap);
        if(userEntities.size()==0){
            return 1;
        }
        List<UserInfo> userInfoLists = new ArrayList<UserInfo>();
        for (UserEntity userEntity : userEntities) {
            UserInfo userInfo = new UserInfo();
            BeanUtils.copyProperties(userEntity,userInfo);
            userInfoLists.add(userInfo);
        }
        response.setUserInfoList(userInfoLists);
        return 0;
    }
    
    
    public int addUser(AddUserRequest request) {
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(request,userEntity);
        return userDao.insertSelective(userEntity);
    }


    public int editUser(EditUserRequest request) {
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(request,userEntity);
        userEntity.setUserId(Integer.parseInt(request.getUserId()));
        return userDao.updateByPrimaryKeySelective(userEntity);
    }


    public int deleteUser(String userId) {
       if(StringUtils.isBlank(userId)){
           return 1;
       }
       return userDao.deleteByPrimaryKey(Integer.parseInt(userId));
    }

}
